package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Product;
import cn.wolfcode.crm.mapper.ProductMapper;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.IProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class ProductService implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Override
    public void saveOrUpdate(Product product) {
        if(product.getId() != null){
            productMapper.updateByPrimaryKey(product);
        } else {
            productMapper.insert(product);
        }
    }

    @Override
    public void delete(Long id) {
        if(id != null){
            productMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public Product getProductById(Long id) {
        return productMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Product> getAll() {
        return productMapper.selectAll();
    }

    @Override
    public PageInfo<Product> selectByQuery(QueryObject queryObject) {
//        int totalCount = productMapper.countByQuery(queryObject);
//        if(totalCount == 0){
//            return new PageResult(1,queryObject.getPageSize(),0, Collections.emptyList());
//        }
        //用pageInfo   调用方法
        PageHelper.startPage(queryObject.getCurrentPage(),queryObject.getPageSize());
        List<Product> products = productMapper.selectByQuery(queryObject);
        return new PageInfo<>(products);
    }

    @Override
    public BigDecimal selectPriceById(Long id) {
        return productMapper.selectPriceById(id);
    }
}
